const dayjs = require('dayjs')

const getTime = date => {
  const now = dayjs().format('YYYY-MM-DD HH:mm')
  return date ? parseInt(new Date(date).getTime()) : parseInt(new Date(now).getTime())
}
const defaultDate = [getTime() - 3600 * 1000 * 3, getTime()] // 默认最近3小时

// 初始化，如果有缓存则用缓存
const state = {
  dates: defaultDate
}

const mutations = {
  UPDATE_DATES: (state, dates) => {
    if (dates) {
      state.dates = typeof dates[0] === 'number' ? dates : [getTime(dates[0]), getTime(dates[1])]
    } else {
      state.dates = defaultDate
    }
  }
}

const actions = {
  UPDATEDATES({ commit }, dates) {
    commit('UPDATE_DATES', dates)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
